<?php
/**
 * 
 * 更新用户教育信息
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","update_user_mblog_count");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
	
	$sql = "select count(*) as count from user_info where count_mblog > 0";
	$db_base = new db_base();
	$count = $db_base->select($sql);
	$count = $count[0]["count"];
	echo "tag_count:$count<br>\n";
	$total_page = ceil($count/1000);
	
	echo "page:$total_page";
	
	for($i=1;$i<=$total_page;$i++)
	{
		get_list_by_page($i);
	}
	
	function get_list_by_page($p)
	{
		$start = ($p-1)*1000;
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$sql = "select uid from user_info where count_mblog > 0 limit  $start,1000" ;
		
		$db_base = new db_base();
		$list = $db_base->select($sql);
		
		$uid_array = array();
		foreach($list as $value)
		{
			$uid = $value["uid"];
			
			
			$sql = "select count(topic_id) as count,topic_id from mblog_pic_content where create_uid = {$uid}  group by topic_id";
			$list = $db_base->select($sql);
			$sql = "update user_search set topic_count = 0 where uid = $uid";
			$db_base->query($sql);
			
			$max_value = 0;
			$count= 0;
			$main_topic_id =0;
			foreach($list as $key => $value)
			{
				$sql = "update user_search set topic_count={$value["count"]} where uid = {$uid} and topic_id={$value["topic_id"]}";
				$db_base->query($sql);
				
				if($max_value < $value["count"])
				{
					$max_value = $value["count"];
					$main_topic_id = $value["topic_id"];
					
				}
				$count +=$value["count"];
			}
			
			if($main_topic_id)
			{
				$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($main_topic_id);
				$main_topic = $topic_info["topic"];
			}
				
			
			$sql = "update user_info set count_mblog = $count,main_topic_id=$main_topic_id,main_topic='{$main_topic}' where uid = '{$uid}'";
			$res = $db_base->query($sql);
		}
	}

